package com.cy.store.controller;

import com.cy.store.controller.ex.*;
import com.cy.store.entity.User;
import com.cy.store.service.IUserService;
import com.cy.store.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * @author Luo Sheng
 * @school YiBin university
 * @create 2022-11-22 18:53
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController extends BaseController{
    @Autowired
    private IUserService userService;

    /**
     * 用户注册方法
     * @param user
     * @return
     */
    @RequestMapping("/reg")
    public JsonResult<Void> reg(User user){
        // 执行注册方法，如果报错会被基类直接检测到并处理
        userService.reg(user);

        // 方法执行成功
        // 创建响应结果对象
        log.info("用户注册成功，用户名：{}",user.getUsername());
        JsonResult<Void> result = new JsonResult<>();
        result.setState(OK);
        result.setMessage("用户注册成功");
        return result;
    }

    /**
     * 用户登陆方法
     * @param user
     * @return 返回一个user对象
     */
    @RequestMapping("/login")
    public JsonResult<User> login(User user,
                                  HttpSession session){
        // 执行业务层登陆方法
        User result = userService.login(user);

//        log.info("resultUid={}",result.getUid());

        // 向Session对象中完成数据的绑定（session全局的）
        session.setAttribute("uid",result.getUid());
        session.setAttribute("username",result.getUsername());

//        log.info("uid={}",session.getAttribute("uid"));

        // 返回JsonResult<User>对象
        return new JsonResult<User>(OK,result);
    }

    /**
     * 修改用户密码
     * @param oldPassword 老的密码
     * @param newPassword 新的密码
     * @param session
     * @return
     */
    @RequestMapping("/change_password")
    public JsonResult<Void> changPassword(String oldPassword,
                                          String newPassword,
                                          HttpSession session){
        // 获取当前登陆用户uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);

        // 调用业务层方法修改password
        userService.changePassword(uid,username,oldPassword,newPassword);

        return new JsonResult<Void>(OK);
    }

    /**
     * 根据session中存储的uid来查询用户信息
     * @param session
     * @return JsonResult<User>用户信息
     */
    @RequestMapping("/get_by_uid")
    public JsonResult<User> getByUid(HttpSession session){
        // 查询并返回用户信息
        User data = userService.getByUid(getUidFromSession(session));
        return new JsonResult<User>(OK,data);
    }


    @RequestMapping("/change_info")
    public JsonResult<Void> changeInfo(User user,
                                       HttpSession session){
        // user对象由四部分数据：username,phone,email,gender
        // uid数据需要再次封装到user对象中
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);

        // 调用业务层方法
        userService.changeInfo(uid,username,user);
        return new JsonResult<>(OK);

    }


    /**
     *
     * @param session
     * @param file
     * @return
     */
    @RequestMapping("/change_avatar")
    public JsonResult<String> changeAvatar(HttpSession session,
                                           MultipartFile file){
        // 上传文件
        String avatarName = upload(file,session);

        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务层的方法
        userService.changeAvatar(uid,avatarName,username);

        // 返回用户的头像路径给前端页面将来用于头像的展示使用
        return new JsonResult<String>(OK,avatarName);
    }



}
